package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author np
 * @Date 2020/10/8 18:29
 * @Version 1.0
 * @Description: 权限
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference //通过dubbo到zookeeper服务中心去查找服务
    private PermissionService permissionService;

    @RequestMapping("/findPage")//请求路径
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return permissionService.findPage(queryPageBean);
    }

    @RequestMapping("/add")//请求路径
    public Result add(@RequestBody Permission permission) {

        try {
            permissionService.add(permission);//真正的业务方法,调用之后新增成功没成功用try...catch...,企业常用
            //超时,服务挂了,zk挂了等
            //通过这个接口远程调用dubbo服务,如果失败就进入到catch打印异常信息
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            //需要返回一个结果
            //返回Result有一个两个参数的构造,标记false告诉他执行结果为失败,并且返回结果常量,结果常量已经在结果常量类定义好
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
        //如果没进catch就是成功同样返回执行成功的结果常量
        return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }
    @RequestMapping("/delete")//请求路径
    public Result delete(Integer id) {//根据传过来的id删除
        try {
            permissionService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Permission permission = permissionService.findById(id);//发送请求
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try{
            permissionService.edit(permission);//发送请求
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }


    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Permission> list = permissionService.findAll();//发送请求
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


}
